package pl.gda.pg.eti.mkdg.projekt;

import java.awt.*;
import javax.imageio.*;
import javax.swing.*;
import javax.swing.JSpinner.NumberEditor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.*;
import java.io.*;
import java.text.*;

public class Main
{

    private BufferedImage imgIn;
    private BufferedImage imgOut;

    private JFrame        frame;
    public JScrollPane    scrollPaneIn;
    public JScrollPane    scrollPaneOut;
    public JButton        buttonLoad;
    public JButton        buttonTest;
    public JLabel         labelIn;
    public JLabel         labelOut;
    public JLabel         label;
    public JLabel         label_1;
    public JLabel         label_2;
    public JLabel         label_3;
    public JLabel         label_4;
    public JLabel         label_5;
    public JLabel         label_6;
    public JLabel         label_7;
    public JLabel         label_8;
    public JLabel         label_10;
    public JToggleButton  fix;
    public JSpinner       spinnerRozmiar;
    public JSpinner       spinnerLambda;
    public JSpinner       spinnerTheta;
    public JSpinner       spinnerPsi;
    public JSpinner       spinnerGamma;
    public JSpinner       spinnerSigma;
    public JLabel         label_9;
    public JLabel         labelPodglad;

    /**
     * Launch the application.
     */
    public static void main(String[] args)
    {
        try
        {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }
        catch (Throwable e)
        {
            e.printStackTrace();
        }
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    Main window = new Main();
                    window.frame.setVisible(true);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Main()
    {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize()
    {
        frame = new JFrame();
        frame.setResizable(false);
        frame.setBounds(100, 100, 1000, 700);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        scrollPaneIn = new JScrollPane();
        scrollPaneIn.setBounds(10, 30, 400, 400);
        frame.getContentPane().add(scrollPaneIn);

        labelIn = new JLabel("");
        labelIn.setHorizontalAlignment(SwingConstants.CENTER);
        scrollPaneIn.setViewportView(labelIn);

        scrollPaneOut = new JScrollPane();
        scrollPaneOut.setBounds(584, 30, 400, 400);
        frame.getContentPane().add(scrollPaneOut);

        labelOut = new JLabel("");
        labelOut.setHorizontalAlignment(SwingConstants.CENTER);
        scrollPaneOut.setViewportView(labelOut);

        scrollPaneOut.getHorizontalScrollBar().setModel(scrollPaneIn.getHorizontalScrollBar().getModel());
        scrollPaneOut.getVerticalScrollBar().setModel(scrollPaneIn.getVerticalScrollBar().getModel());

        buttonLoad = new JButton("Wczytaj obraz");
        buttonLoad.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
                buttonLoadActionPerformed(arg0);
            }
        });
        buttonLoad.setBounds(420, 30, 154, 23);
        frame.getContentPane().add(buttonLoad);

        buttonTest = new JButton("Filtr kwadraturowy");
        buttonTest.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                buttonTestActionPerformed(e);
            }
        });
        buttonTest.setBounds(10, 441, 154, 23);
        frame.getContentPane().add(buttonTest);

        label = new JLabel("Obraz wej\u015Bciowy:");
        label.setBounds(10, 5, 100, 20);
        frame.getContentPane().add(label);

        label_1 = new JLabel("Obraz wyj\u015Bciowy:");
        label_1.setBounds(584, 5, 100, 20);
        frame.getContentPane().add(label_1);

        label_2 = new JLabel("Parametry filtru kwadraturowego (Gabora):");
        label_2.setBounds(10, 472, 226, 14);
        frame.getContentPane().add(label_2);

        label_3 = new JLabel("Rozmiar ramki:");
        label_3.setBounds(10, 497, 154, 14);
        frame.getContentPane().add(label_3);

        label_4 = new JLabel("D\u0142ugo\u015B\u0107 fali (\u03BB):");
        label_4.setBounds(10, 522, 154, 14);
        frame.getContentPane().add(label_4);

        label_5 = new JLabel("Orientacja (\u03B8):");
        label_5.setBounds(10, 547, 154, 14);
        frame.getContentPane().add(label_5);

        label_6 = new JLabel("Przesuni\u0119cie (\u03C6):");
        label_6.setBounds(10, 572, 154, 14);
        frame.getContentPane().add(label_6);

        label_7 = new JLabel("Stosunek proporcji (\u03B3):");
        label_7.setBounds(10, 597, 154, 14);
        frame.getContentPane().add(label_7);

        label_8 = new JLabel("\u015Arednica dzwonu Gaussa (\u03C3):");
        label_8.setBounds(10, 622, 154, 14);
        frame.getContentPane().add(label_8);

        label_10 = new JLabel("Wyróżnienie ekstremów:");
        label_10.setBounds(10, 652, 154, 14);
        frame.getContentPane().add(label_10);
        
        fix = new JToggleButton();
        fix.setSelected(false);
        fix.setBounds(176, 652, 14, 14);
        frame.getContentPane().add(fix);

        spinnerRozmiar = new JSpinner();
        spinnerRozmiar.setModel(new SpinnerNumberModel(31, 21, 71, 2));
        spinnerRozmiar.setBounds(176, 495, 60, 20);
        frame.getContentPane().add(spinnerRozmiar);

        spinnerLambda = new JSpinner();
        NumberEditor editor = (NumberEditor) spinnerLambda.getEditor();
        DecimalFormat format = editor.getFormat();
        format.setMinimumFractionDigits(2);
        spinnerLambda.setModel(new SpinnerNumberModel(new Double(5), new Double(2), null, new Double(1)));
        spinnerLambda.setBounds(176, 520, 60, 20);
        frame.getContentPane().add(spinnerLambda);

        spinnerTheta = new JSpinner();
        editor = (NumberEditor) spinnerTheta.getEditor();
        format = editor.getFormat();
        format.setMinimumFractionDigits(2);
        spinnerTheta.setModel(new SpinnerNumberModel(0.0, 0.0, 360.0, 1.0));
        spinnerTheta.setBounds(176, 545, 60, 20);
        frame.getContentPane().add(spinnerTheta);

        spinnerPsi = new JSpinner();
        editor = (NumberEditor) spinnerPsi.getEditor();
        format = editor.getFormat();
        format.setMinimumFractionDigits(2);
        spinnerPsi.setModel(new SpinnerNumberModel(0.0, -180.0, 180.0, 1.0));
        spinnerPsi.setBounds(176, 570, 60, 20);
        frame.getContentPane().add(spinnerPsi);

        spinnerGamma = new JSpinner();
        editor = (NumberEditor) spinnerGamma.getEditor();
        format = editor.getFormat();
        format.setMinimumFractionDigits(2);
        spinnerGamma.setModel(new SpinnerNumberModel(0.5, 0.0, 1.0, 0.0));
        spinnerGamma.setBounds(176, 595, 60, 20);
        frame.getContentPane().add(spinnerGamma);

        spinnerSigma = new JSpinner();
        editor = (NumberEditor) spinnerSigma.getEditor();
        format = editor.getFormat();
        format.setMinimumFractionDigits(2);
        spinnerSigma.setModel(new SpinnerNumberModel(new Double(8), null, null, new Double(1)));
        spinnerSigma.setBounds(176, 620, 60, 20);
        frame.getContentPane().add(spinnerSigma);

        label_9 = new JLabel("Podgl\u0105d filtru:");
        label_9.setBounds(282, 472, 127, 14);
        frame.getContentPane().add(label_9);

        labelPodglad = new JLabel("");
        labelPodglad.setVerticalAlignment(SwingConstants.TOP);
        labelPodglad.setBounds(282, 495, 145, 145);
        frame.getContentPane().add(labelPodglad);
    }

    protected void buttonLoadActionPerformed(ActionEvent e)
    {
        File file = null;
        JFileChooser fc = new JFileChooser();

        int ret = fc.showOpenDialog(frame);

        if (ret == JFileChooser.APPROVE_OPTION)
            file = fc.getSelectedFile();
        else
            return;

        BufferedImage img = null;
        try
        {
            img = ImageIO.read(file);
        }
        catch (IOException e1)
        {
            return;
        }

        FragmentSelector fs = new FragmentSelector(this);
        fs.OpenWithImage(img);
    }

    public void LoadImage(BufferedImage img)
    {
        imgIn = img;
        imgOut = new BufferedImage(imgIn.getWidth(), imgIn.getHeight(), imgIn.getType());
        imgOut.setData(imgIn.getData());

        labelIn.setIcon(new ImageIcon(imgIn));
        labelOut.setIcon(new ImageIcon(imgOut));
    }

    protected void buttonTestActionPerformed(ActionEvent e)
    {
        if (imgIn == null)
            return;

        QuadratureFilter f = new QuadratureFilter((Integer) spinnerRozmiar.getValue(), (Double) spinnerLambda.getValue(), (Double) spinnerTheta.getValue(), (Double) spinnerPsi.getValue(), (Double) spinnerSigma.getValue(), (Double) spinnerGamma.getValue());

        labelPodglad.setIcon(new ImageIcon(f.miniature));
        labelOut.setIcon(new ImageIcon(f.filter(imgIn, fix.isSelected())));
    }
}
